package com.ptu.service.impl;
import com.ptu.entity.Duty;
import com.ptu.entity.Employee;
import com.ptu.entity.PageTools;
import com.ptu.mapper.DutyMapper;
import com.ptu.mapper.EmployeeMapper;
import com.ptu.service.DutyService;
import com.ptu.util.DBUtil;
import org.apache.ibatis.annotations.Param;
import java.sql.Date;
import java.sql.Time;
import java.util.List;

public class DutyServiceImpl implements DutyService {


    @Override
    public int addDuty(String eID, Date date1, Time time1) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        int flag=dutyMapper.addDuty(eID,date1.toString(),time1.toString());
        DBUtil.closeAll();
        return flag;

    }

    @Override
    public int updateDuty(String eID,Date date1,Time time1) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        int flag=dutyMapper.updateDuty(eID,date1.toString(),time1.toString());
        DBUtil.closeAll();
        return flag;
    }

    @Override
    public List<Duty> queryDutes() {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        List dutes=dutyMapper.queryDutes();
        DBUtil.closeAll();
        return  dutes;
    }

    @Override
    public Duty queryDuty(String eID,String date1) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        Duty duty=dutyMapper.queryDuty(eID,date1.toString());
        DBUtil.closeAll();
        return duty;
    }

    @Override
    public String  isCheckin(String eID,  String date1, String time){
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        String flag=dutyMapper.isCheckin(eID.toString(),date1.toString(),time.toString());
        DBUtil.closeAll();
        return flag;
    }

    @Override
    public String isCheckout(String eID,  String date1, String time){

        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        String flag=dutyMapper.isCheckout(eID,date1.toString(),time.toString());
        DBUtil.closeAll();
        return flag;
    }

    @Override
    public Duty test(String eID) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        Duty t=dutyMapper.test("1");
        DBUtil.closeAll();
        return t;
    }

    @Override
    public PageTools<Duty> findMyDuty(String eID,int pageSize, int pageNumber) {
        DutyMapper mapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);

        PageTools<Duty> pageTools = new PageTools<>();
        pageTools.setPageNumber(pageNumber);
        pageTools.setPageSize(pageSize);

        int pageStart = pageSize*(pageNumber-1);
        List<Duty> duties = mapper.getMyDuty(eID,pageStart, pageSize);
        int total = mapper.myDutyNum(eID);
        pageTools.setTotal(total);
        pageTools.setList(duties);

        DBUtil.closeAll();
        return pageTools;
    }

    @Override
    public Duty getDutyByID(String eID) {
        return null;
    }

    @Override
    public PageTools<Duty> findByIDorDate(String eID,String date,int pageSize, int pageNumber) {
        DutyMapper mapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);

        PageTools<Duty> pageTools = new PageTools<>();
        pageTools.setPageNumber(pageNumber);
        pageTools.setPageSize(pageSize);
        List<Duty> duties=null;
        int total = 0;
        int pageStart = pageSize*(pageNumber-1);
        if((!(eID==null || date==null))&&(!(date==null || date==""))){
             duties = mapper.ByDateAndID(eID,date,pageStart, pageSize);
             total=mapper.getCountByDateAndID(date,eID);
        }
        if((!(eID==null || date==null))&&(date==null || date=="")){
             duties=mapper.getMyDuty(eID,pageStart,pageSize);
             total=mapper.myDutyNum(eID);
        }
        if((eID==null || eID=="")&&!(date==null || date=="")){
             duties=mapper.getDutyByDate(date,pageStart,pageSize);
             total=mapper.getCountByDate(date);
        }
        pageTools.setTotal(total);
        pageTools.setList(duties);
        DBUtil.closeAll();
        return pageTools;
    }

    @Override
    public PageTools<Duty> findAllDuty(int pageSize, int pageNumber) {
        DutyMapper mapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);

        PageTools<Duty> pageTools = new PageTools<>();
        pageTools.setPageNumber(pageNumber);
        pageTools.setPageSize(pageSize);

        int pageStart = pageSize*(pageNumber-1);
        List<Duty> duties = mapper.findAllDuty(pageStart,pageSize);
        int total=mapper.AllDutyNum();
        pageTools.setTotal(total);
        pageTools.setList(duties);

        DBUtil.closeAll();
        return pageTools;
    }

    @Override
    public List<Duty> findMore(String empid, String deptno, String dtdate) {
        DutyMapper mapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);

        List<Duty> list = mapper.selectMore(empid, deptno, dtdate);

        DBUtil.closeAll();

        return list;
    }


    @Override
    public List<Duty> getMyDuty(String eID,Integer currIndex,Integer pageSize) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        List<Duty> t=dutyMapper.getMyDuty(eID,currIndex,pageSize);
        DBUtil.closeAll();
        return t;
    }

    @Override
    public int myDutyNum(String eID) {
        DutyMapper dutyMapper = DBUtil.getSqlSession().getMapper(DutyMapper.class);
        int num=dutyMapper.myDutyNum(eID);
        DBUtil.closeAll();
        return num;
    }
}
